package com.fancy.learncenter.ui.view;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.fancy.learncenter.R;
import com.fancy.learncenter.common.MAudioPlayer;
import com.fancy.learncenter.common.MyApplication;
import com.fancy.learncenter.utils.LogUtil;
import com.fancy.learncenter.utils.TimeUtil;

/**
 * Created by Hyy on 2017/2/24.
 */

public class EnterCartoonPopupWindow extends BasePopupWindow {
    View locationView;

    Activity mContext;

    public EnterCartoonPopupWindow(Activity mContext, View locationView) {
        super(mContext);
        this.locationView = locationView;
        this.mContext = mContext;
        initPopup();

    }

    View popopView;
    SimpleDraweeView simpleDraweeView;
    ProgressBar recordPb;

    private void initPopup() {
        popopView = LayoutInflater.from(MyApplication.context).inflate(R.layout.popupwindow_enter_cartoon, null);

        recordPb = (ProgressBar) popopView.findViewById(R.id.record_pb);
        simpleDraweeView = (SimpleDraweeView) popopView.findViewById(R.id.loading);

        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.MATCH_PARENT);

        setOutsideTouchable(false);

        String urlPath = "res://" + mContext.getPackageName() + "/" + R.mipmap.cartoon_enter_loding;
        DraweeController draweeController =
                Fresco.newDraweeControllerBuilder()
                        .setUri(urlPath)
                        .setOldController(simpleDraweeView.getController())
                        .setAutoPlayAnimations(true) // 设置加载图片完成后是否直接进行播放
                        .build();
        simpleDraweeView.setController(draweeController);

        popopView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });


        setContentView(popopView);
    }

    @Override
    public void showPopupWindow() {
        super.showPopupWindow();
        showAtLocation(locationView, Gravity.CENTER, 0, 0);
//        showAsDropDown(locationView, Gravity.CENTER, 0, 0);
//        showAtLocation(locationView, Gravity.CENTER, 0, 0);
//        timer();

        playMusic();
        timer();
    }

    MAudioPlayer recordPlayer;

    private void playMusic() {
        recordPlayer = new MAudioPlayer(mContext, R.raw.cartoom_home_process);
        recordPlayer.setAudioPlayerStatus(new MAudioPlayer.AudioPlayerStatus() {
            @Override
            public void playerPause() {
            }

            @Override
            public void playerCompletion(MediaPlayer mp) {
                dismiss();
            }

            @Override
            public void playerPrepared(MediaPlayer mp) {
            }

            @Override
            public void playerStart() {
            }

            @Override
            public void playerError(MediaPlayer mp, int what, int extra) {
            }
        });
        recordPlayer.play();
    }

    private void timer() {


        TimeUtil.interval(30, new TimeUtil.TimeCallBack() {
            /**
             * 计时中
             *
             * @param time
             */
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void nextCallBack(long time) {
                recordPb.setProgress((int) time);
                if (time >= 103) {
                    TimeUtil.cancle();


                } else {
                    if (time == 10) {

                    }
                }
            }

            /**
             * 计时完成
             */
            @Override
            public void completeCallBack() {
                LogUtil.MyLog("videoPlayers", "=======completeCallBack========");
            }

            /**
             * 计时开始
             */
            @Override
            public void startCallBack() {
                LogUtil.MyLog("videoPlayers", "=======startCallBack========");
            }
        });
    }

    @Override
    public void dismiss() {
        super.dismiss();
        recordPlayer.stop();
        if (disCallBack != null) {
            disCallBack.disCallBack();
        }
    }


    private DisCallBack disCallBack;

    public DisCallBack getDisCallBack() {
        return disCallBack;
    }

    public void setDisCallBack(DisCallBack disCallBack) {
        this.disCallBack = disCallBack;
    }

    public interface DisCallBack {
        abstract void disCallBack();
    }
}
